package game.scenario;

import game.player.Player;
import game.units.SimpleUnit;
import game.units.Unit;
import game.units.UnitPool;

import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Iterator;
import java.util.Vector;

import utils.DBG;
import core.pathfinder.RoutePoint;

public class Wave extends UnitPool {

	Vector<Unit> victorious;
	Vector<Unit> deletion;
	Point begin;
	Point end;
	boolean needsRecalculation = false;
	public Wave(int num_units, Point begin_point , Point end_point) {
		super(10, begin_point , end_point);
		begin = begin_point;
		end = end_point;
		victorious = new Vector<Unit>();
		deletion = new Vector<Unit>();
		this.updateRoute();
	}

	@Override
	public void addUnit(Unit u) {
		// TODO Auto-generated method stub
		super.addUnit(u);
	}

	@Override
	public Unit getClosest(Unit other) {
		// TODO Auto-generated method stub
		return super.getClosest(other);
	}

	@Override
	public void renderComponent(Graphics2D g2d) {
		// TODO Auto-generated method stub
		Iterator<Unit> it = this.iterator();
		while (it.hasNext()) {
			it.next().render(g2d);
		}
	}

	@Override
	public void updateLogic() {
		if (this.needsRecalculation){
			this.updateRoute();
		}
		this.doGarbage();
		// TODO Auto-generated method stub
		Iterator<Unit> it = this.iterator();
		Player pl = Player.getInstance();
		while (it.hasNext()) {
			Unit u = it.next();
			if (u instanceof SimpleUnit) {
				SimpleUnit su = (SimpleUnit) u;
				su.updateLogic();
				if (su.needNewObjective) {
					RoutePoint newObjective = su.getRoute().getNextPoint(su
							.getObjective());

					if (newObjective == null) {
						victorious.add(su);
						pl.looseLive();
						DBG.showMsg("Player loosed live [ " + pl.getLives()
								+ " ] Remaining ", this);
						deletion.add(su);
					} else {
						su.setObjective(newObjective);
					}
				}
			}
		}
	}
	
	Vector<Unit> update = null;
	
	public synchronized void updateRoute(){
		if ( update == null || update.isEmpty()){
			update = null;
			update = new Vector<Unit>(this.getUnits());
			this.needsRecalculation = false;
		}else{
	
			SimpleUnit u = (SimpleUnit)update.get(0);
			if(u != null){
				u.recalculateRoute();
				update.remove(0);
			}
		}
	}

	
	
	private void doGarbage() {
		Iterator<Unit> itd = deletion.iterator();
		while (itd.hasNext()) {
			this.remove(itd.next());
		}
		deletion.clear();

		this.clean();
	}

	public void clean() {
		Vector<Unit> dead = this.getDeathUnits();
		Iterator<Unit> it = dead.iterator();
		while (it.hasNext()) {
			Unit u = it.next();
			Player.getInstance().addCash(u.getMoney());
			DBG.showMsg("Player earned [ " + u.getMoney() + " ] [ "
					+ Player.getInstance().getCash() + " ] remaining.", this);
			this.remove(u);
		}
	}

	public int getObtainedMoney() {
		int retorno = 0;

		Iterator<Unit> it = this.victorious.iterator();

		while (it.hasNext()) {
			retorno += it.next().getMoney();
		}

		return retorno;
	}

}
